#!/usr/bin/env python

"""
 UDP server
"""

import socket

host = socket.gethostname()
port = 8080

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((host, port))  # UDP 服务器需绑定端口，无需 accept()

    print("UDP 服务器启动，等待客户端消息...")
    
    while True:
        data, addr = s.recvfrom(1024)  # 接收客户端数据和地址
        print(f"收到 {addr} 的消息：{data}")
        if data == 'quit':
            s.sendto('quit', addr)  # 发送退出指令
            break
        s.sendto("Received: " + data, addr)  # 回复客户端